<template>
	<view>
		<view v-if="empty" class="empty">
			<text>暂无数据，快去收藏喜欢的店铺吧 ~</text>
		</view>
		<List v-else title="收藏列表" :shops="shops" :loading="loading" />
	</view>
</template>

<script>
	import List from "../../components/List.vue"

	const db = wx.cloud.database();

	export default {
		openId: uni.getStorageSync('openid'),

		components: {
			List
		},

		data() {
			return {
				shops: [],
				loading: false,
				empty: true,
			}
		},

		onShow() {
			this.showCollected();
		},

		methods: {
			async showCollected() {
				this.loading = true;
				const collected = await db.collection('favorList')
					.where({
						openid: this.$options.openId
					})
					.get();

				const ids = [];
				collected.data.forEach(item => {
					ids.push(item.id);
				});

				db.collection('shopList')
					.where({
						id: db.command.in(ids)
					})
					.get()
					.then(res => {
						if(res.data.length){
							res.data.forEach(item => {
								if (!item.image_path) {
									item.image_path = 'mdata/img/404.jpg';
								}
							})
							this.shops = res.data;
							this.loading = false;
							this.empty = false;
						}
					})
			}
		}
	}
</script>
<style>
	.empty {
		margin-top: 30%;
		color: #515B67;
		text-align: center;
	}
</style>
